Ext.define('console.evalPageWindow', {
    extend: 'Ext.ux.desktop.Module',

    requires: [
        'console.store.deptStore',
        'console.store.questionStore',
        'Ext.data.ArrayStore',
        'Ext.util.Format',
        'Ext.grid.Panel',
        'Ext.grid.RowNumberer'
    ],

    id: 'evalPageWindow',
    init: function () {
        this.launcher = {
            text: '满意度调查表',
            iconCls: 'icon-grid'
        };
    },
    createWindow: function () {
        window.open("deptMapper/selectDeptUpcomingPaperPage");
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('evalPageWindow');
        if (!win) {
            var content = Ext.createWidget('tabpanel', {
                activeTab: 0,
                width: '95%',
                height:  '90%',
                tabPosition: 'bottom'           //指定了选项卡的位置，left,right
            });
            var desktop = this.app.getDesktop();
            var win = desktop.getWindow('evalPageWindow');
            if (!win) {
                win = desktop.createWindow({
                    id: 'evalPageWindow',
                    title: '评价表填报:' + Ext.util.Format.date(new Date(), 'Y-m') + '月份',
                    width: 1024,
                    height: 650,
                    iconCls: 'icon-grid',
                    animCollapse: false,
                    layout: 'fit',
                    constrainHeader: true,
                    items: content
                });
            }
            this.initialTabs(content);
            return win;
        }
    },
    initialTabs: function (tabs) {
        Ext.Ajax.request({
            url: 'eval/loadEvalPageTabs',
            success: function (response) {
                var text = response.responseText;
                var res = Ext.decode(text);
                var list = res.data;
                if (list.length > 0) {
                    for (var i = 0; i < list.length; i++) {
                        var el = new Object(list[i]);
                        var tab = tabs.add({
                            xtype: 'panel',
                            //title: el.groupName + '评价填报', //Fun_hanxn190321_mark
                            title: '试卷' + el.paperId + '评价填报', //Fun_hanxn190321_mod
                            deptCode: el.deptCode,
                            //groupCode: el.groupCode, //Fun_hanxn190321_mark
                            paperId: el.paperId, //Fun_hanxn190321_add
                            closable: false,
                            layout: "border",
                            items: [{
                                xtype: 'grid',
                                region: "north",
                                title: '问题列表',
                                height: '24%',
                                columns: [new Ext.grid.RowNumberer(), {
                                    text: "题号",
                                    width: '10%',
                                    sortable: true,
                                    hidden: true,
                                    dataIndex: 'id'
                                }, {
                                    text: "内容",
                                    width: '80%',
                                    sortable: true,
                                    dataIndex: 'content'
                                }, {
                                    text: "备注",
                                    flex: 1,
                                    sortable: true,
                                    dataIndex: 'mark'
                                }],
                                store: Ext.createWidget('questionStore', {autoLoad: false})
                            },
                                {
                                    xtype: 'panel',
                                    itemId: 'evalPageList',
                                    region: "center",
                                    title: '填报'/* ,
                                    autoScroll: true,*/
                                }
                            ],
                            listeners: {
                                activate: function (t, e) {
                                    var params = {
                                        deptCode: t.deptCode,
                                        //groupCode: t.groupCode, //Fun_hanxn190321_mark
                                        paperId: t.paperId //Fun_hanxn190321_mod
                                    }
                                    t.query("grid")[0].store.load({params: params});
                                    Ext.Ajax.request({
                                        url: 'eval/loadEvalPageContent',
                                        params: params,
                                        success: function (response) {

                                            var res = Ext.decode(response.responseText).data;
                                            var questions = res.questions;
                                            var scoreCombobox = {
                                                xtype: 'combobox',
                                                editable: false,
                                                store: Ext.data.StoreManager.lookup('gradeScoreStore'),
                                                triggerAction : 'all',
                                                queryMode: 'remote',
                                                displayField: 'name',
                                                valueField: 'code',
                                                listeners: {
                                                    focus: function( i, e, o) {
                                                        i.expand();
                                                    }
                                                },
                                                allowBlank: false
                                            };

                                            var fields = new Array();
                                            fields.push("masterId");
                                            fields.push("targetDeptCode");
                                            fields.push("targetDeptName");
                                            var columns = new Array();
                                            columns.push({
                                                "dataIndex": "id",
                                                "width": 50,
                                                flex: 1,
                                                hidden: true,
                                                "text": "评价主记录代码"
                                            });
                                            columns.push({
                                                "dataIndex": "targetDeptCode",
                                                "width": 50,
                                                flex: 1,
                                                "text": "科室代码"
                                            });
                                            columns.push({
                                                "dataIndex": "targetDeptName",
                                                "width": 50,
                                                flex: 1,
                                                "text": "科室名称"
                                            });
                                            for (var s = 0; s < questions.length; s++) {
                                                //var q;
                                                //if(s==0) q="履行自身职责水平";
                                                //else  if(s==1) q="支持配合的主动性";
                                                //else  if(s==2) q="工作沟通的通畅性";
                                                //else  if(s==3) q="工作及时性";
                                                //else  if(s==4) q="工作支持配合效果"; //Fun_hanxn_20190321_mark
                                                columns.push({
                                                    "dataIndex": questions[s].id,
                                                    //"text": q, //Fun_hanxin_20190321_mark
                                                    "text": "第 " + (s + 1) + " 个问题", //Fun_hanxn_20190321_add
                                                    flex: 1,
                                                    editor: scoreCombobox,
                                                    renderer: function (val) {
                                                        if (val == null || val == "") {
                                                        } else {
                                                            return Ext.data.StoreManager.lookup('gradeScoreStore').findRecord('code', val).get('name');
                                                        }
                                                    }
                                                });
                                                fields.push(questions[s].id + "");
                                            }
                                            fields.push("mark");
                                            columns.push({
                                                "dataIndex": "mark",
                                                "width": 50,
                                                flex: 1,
                                                "text": "备注",
                                                editor: {
                                                    xtype: 'textfield',
                                                    name: 'mark'
                                                }
                                            });
                                            t.child("#evalPageList").removeAll(true);
                                            t.child("#evalPageList").add({
                                                xtype: 'grid',
                                                columns: columns,
                                                columnLines: true,
                                                rowLines: true,
                                                height:425,
                                                autoScroll: true,
                                                selType: 'cellmodel',
                                                store: Ext.create("Ext.data.Store", {
                                                    fields: fields,
                                                    data: res.masters

                                                }),
                                                tbar: [{
                                                    text: '总提交',
                                                    tooltip: '提交',
                                                    iconCls: 'submit',
                                                 handler : function() {
                                                    Ext.Ajax.request({
                                                            url: 'eval/submitEval',
                                                            params : {
                                                                groupCode:t.groupCode,
                                                                paperId: t.paperId //Fun_hanxn190321_mod
                                                            },

                                                        success: function (response) {
                                                            var res = Ext.decode(response.responseText);
                                                            if (res.success) {
                                                                Ext.Msg.alert('提示信息', '提交成功');

                                                            } else {
                                                                Ext.Msg.alert('提示信息', res.failure);
                                                            }
                                                        },
                                                        failure: function (action) {
                                                            Ext.Msg.alert('提示信息',res.failure);
                                                        }
                                                        });
                                                    }
                                                }],
                                                plugins: [Ext.create('Ext.grid.plugin.CellEditing', {
                                                    clicksToEdit: 1,
                                                    listeners: {
                                                        edit: function (editor, e) {
                                                            if (e.originalValue != e.value) {
                                                                var masterId = e.record.data.id;
                                                                var questionId = e.field;
                                                                var grade = e.value;
                                                                var serialParams = {masterId: masterId, grade: grade,groupCode:t.groupCode};
                                                                if (e.field == "mark") {
                                                                    var mark = e.value;
                                                                    serialParams.mark = mark;
                                                                } else {
                                                                    serialParams.questionId = questionId;
                                                                }
                                                                Ext.Ajax.request({
                                                                    url: 'eval/updateEval',
                                                                    params: serialParams,

                                                                    success: function (response) {
                                                                        var res = Ext.decode(response.responseText);
                                                                        if (res.success) {
                                                                            var res = Ext.decode(response.responseText);
                                                                            e.record.data[e.field] = e.value;
                                                                            e.record.commit();
                                                                        } else {
                                                                            Ext.Msg.alert('提示信息', res.failure);
                                                                        }
                                                                    },
                                                                    failure: function (action) {
                                                                        Ext.Msg.alert('提示信息',res.failure);
                                                                    }
                                                                })
                                                            }
                                                        }
                                                    }
                                                })]
                                            })
                                        }
                                    });
                                }
                            }
                        });
                        if (i == 0) {
                            tab.setActive(true);
                        }
                    }
                }
            }
        });
    },
    initialContentOfTab: function (dept) {
        Ext.Ajax.request({
            url: 'eval/loadEvalPageContent',
            params: dept,
            success: function (response) {
                var text = response.responseText;
                var res = Ext.decode(text);

            }
        });
    }
});

